package com.zwt.util.sms;

import com.alibaba.fastjson.JSONObject;
import com.zwt.redis.Redis;
import com.zwt.redis.TRedis;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/11/3.
 */
@Component
public class MessageUtil {

    private static final Logger log = LoggerFactory.getLogger(MessageUtil.class);



    /**
     * 发送短信
     * @param phone
     * @return
     */
    public static JSONObject sendRandomCode(Phone phone){
        JSONObject result = new JSONObject();
        //判断手机号是否存在
        if(phone==null || StringUtils.isEmpty(phone.getPhone())){
            result.put("code",-1);
            result.put("msg","获取手机号失败,请重新发送！");
            return result;
        }

        //发送次数
        //redis Key
        Integer sendNum = phone.getSendNum();
        String sendNumRedisKey = phone.getSendNumRedisKey() + phone.getPhone();
        //查询当前手机号申请次数
        Object inApplyNumber = TRedis.getStr(sendNumRedisKey);
        Integer modifyNumber = inApplyNumber!=null ? Integer.parseInt(inApplyNumber.toString()) : 0;
        if (phone.getFlag() != null && phone.getFlag()){

        }else {
            if(sendNum!=null && modifyNumber>=sendNum){
                result.put("code",-1);
                result.put("number",modifyNumber);
                result.put("msg","获取次数上限，请次日申请！");
                return result;
            }
        }

        //查询两次发送间隔时间
        //间隔时间key
        String intervalKey = phone.getSendIntervalPeriodKey() + phone.getPhone();
        //是否存在间隔
        Object interval = TRedis.getStr(intervalKey);
        if(interval!=null){
            result.put("code",-1);
            result.put("msg","短信正在发送中，稍候再试！");
            return result;
        }

        //验证码
        String randomCode = phone.getRandomCode();
        //redis Key
        String randomRedisKey = phone.getRandomRedisKey() + phone.getPhone();
        //重复发送时删除上一条redis记录
        if(TRedis.getStr(phone.getPhone())!=null){
            TRedis.del(randomRedisKey);
        }
        //发送短信
        result.put("msg","短信发送失败！");
        int i = TSMS.sendsms(TSMS.MAIN_SMS,phone.getPhone(),phone.getMessage());
        if(1==i){
            if(!StringUtils.isEmpty(phone.getRandomRedisKey())){
                //向redis添加记录 key：指定前缀+手机号 value：验证码
                TRedis.setStr(randomRedisKey,randomCode,phone.getRandomPeriod());
            }
            if(!StringUtils.isEmpty(phone.getSendNumRedisKey())){
                //如果没有指定次数的生命周期，则不设置生命周期。
                Integer sendPeriod = phone.getSendNumPeriod() == null ? -1 : phone.getSendNumPeriod();
                //向redis添加记录 key：指定前缀+手机号 value：次数
                Long number = TRedis.incr(sendNumRedisKey , modifyNumber==0 ? sendPeriod : -1);
                result.put("number",number);
            }
            if(!StringUtils.isEmpty(phone.getSendIntervalPeriodKey())){
                //向redis添加短信间隔校验
                TRedis.setStr(intervalKey,"close",phone.getSendIntervalPeriod());
            }
            log.info("手机号：" + phone.getPhone() + "  验证码已发送到手机(" + randomCode +")");
            result.put("code",0);
            result.put("msg","短信发送成功！");
        }
        return result;
    }

    /**
     * 校验手机验证码
     * @param phone
     * @return
     */
    public static JSONObject verifyRandomCode(Phone phone){
        JSONObject result = new JSONObject();
        result.put("code",-1);
        if(StringUtils.isEmpty(phone.getPhone())){
            result.put("msg","手机号码不能为空");
            return result;
        }
        if(StringUtils.isEmpty(phone.getRandomCode())){
            result.put("msg","手机验证码不能为空");
            return result;
        }
        //获取当前验证码的key
        String randomKey = phone.getRandomRedisKey() + phone.getPhone();
        Object redis = TRedis.getStr(randomKey);
        if(redis==null){
            result.put("msg","手机验证码过期，请重新输入!");
            return result;
        }
        if(!phone.getRandomCode().equals(redis)){
            result.put("msg","手机验证码有误，请重新输入!");
            return result;
        }
        result.put("code",0);
        //验证成功后，删除redis记录
        TRedis.del(randomKey);
        return result;
    }
}
